//
//  ViewController.swift
//  Y0001_SendSMS
//
//  Created by yao_yu on 14/10/28.
//  Copyright (c) 2014年 yao_yu. All rights reserved.
//

import UIKit
import MessageUI

class ViewController: UIViewController, UINavigationControllerDelegate, MFMessageComposeViewControllerDelegate {
    
    var lblResult = UILabel()
    var txtTo = UITextField()
    var txtSMS = UITextField()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupViews()
    }
    
    // 构造界面
    func setupViews(){
        var frame1 = UIView()
        frame1.setTranslatesAutoresizingMaskIntoConstraints(false)
//        frame1.backgroundColor = UIColor.cyanColor()
        view.addSubview(frame1)
        
        var frame2 = UIView()
//        frame2.backgroundColor = UIColor.lightGrayColor()
        frame2.setTranslatesAutoresizingMaskIntoConstraints(false)
        view.addSubview(frame2)
        
        lblResult.setTranslatesAutoresizingMaskIntoConstraints(false)
        lblResult.text = "状态"
        lblResult.numberOfLines = 0
        lblResult.textAlignment = NSTextAlignment.Center
        view.addSubview(lblResult)
        
        var views = ["frame1":frame1, "frame2":frame2, "result":lblResult]
        let layoutOptions = NSLayoutFormatOptions(0)
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-8-[frame1]-8-|", options: layoutOptions, metrics: nil, views: views))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-8-[frame2]-8-|", options: layoutOptions, metrics: nil, views: views))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-8-[result]-8-|", options: layoutOptions, metrics: nil, views: views))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-40-[frame1(30)]-[frame2(30)]-[result]|", options: layoutOptions, metrics: nil, views: views))
        
        
        var lblTo = UILabel()
        lblTo.text = "发送到"
        lblTo.setTranslatesAutoresizingMaskIntoConstraints(false)
        frame1.addSubview(lblTo)
        
        txtTo.setTranslatesAutoresizingMaskIntoConstraints(false)
        txtTo.text = "10001,PY0000"
        txtTo.borderStyle = UITextBorderStyle.RoundedRect
        frame1.addSubview(txtTo)
        views = ["lbl":lblTo, "txt":txtTo]
        frame1.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[lbl(55)]-[txt]-48-|", options: layoutOptions, metrics: nil, views: views))
        frame1.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[lbl]|", options: layoutOptions, metrics: nil, views: views))
        frame1.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[txt]|", options: layoutOptions, metrics: nil, views: views))
        
        var lblSMS = UILabel()
        lblSMS.text = "消息"
        lblSMS.setTranslatesAutoresizingMaskIntoConstraints(false)
        frame2.addSubview(lblSMS)
        
        txtSMS.setTranslatesAutoresizingMaskIntoConstraints(false)
        txtSMS.text = UIDevice.currentDevice().identifierForVendor.UUIDString
        txtSMS.borderStyle = UITextBorderStyle.RoundedRect
        frame2.addSubview(txtSMS)
        
        var btnSend = UIButton.buttonWithType(UIButtonType.System) as UIButton
        btnSend.setTitle("发送", forState: UIControlState.Normal)
        btnSend.setTranslatesAutoresizingMaskIntoConstraints(false)
        btnSend.addTarget(self, action: "sendSMS", forControlEvents: UIControlEvents.TouchUpInside)
        frame2.addSubview(btnSend)
        
        views = ["lbl":lblSMS, "txt":txtSMS, "send":btnSend]
        frame2.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[lbl(55)]-[txt]-[send(40)]|", options: layoutOptions, metrics: nil, views: views))
        frame2.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[lbl]|", options: layoutOptions, metrics: nil, views: views))
        frame2.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[txt]|", options: layoutOptions, metrics: nil, views: views))
        frame2.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[send]|", options: layoutOptions, metrics: nil, views: views))
    }
    
    func sendSMS(){
        if MFMessageComposeViewController.canSendText(){
            let controller = MFMessageComposeViewController()
            controller.body = txtSMS.text
            controller.recipients = txtTo.text.componentsSeparatedByString(",")
            controller.messageComposeDelegate = self
            self.presentViewController(controller, animated: true, completion: nil)
        } else {
            let alert = UIAlertView(title: "提示信息", message: "本设备不能发短信", delegate: nil, cancelButtonTitle: "确定")
            alert.show()
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func messageComposeViewController(controller: MFMessageComposeViewController!, didFinishWithResult result: MessageComposeResult) {
        
        controller.dismissViewControllerAnimated(true, completion: nil)
        
        switch result.value{
        case MessageComposeResultSent.value:
            lblResult.text = "短信已发送"
        case MessageComposeResultCancelled.value:
            lblResult.text = "短信已取消"
        case MessageComposeResultFailed.value:
            lblResult.text = "短信发送失败"
        default:
            break
        }
    }
    
}

